package com.bangdao.app.jetpacktest.jetpack.viewmodel;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModelProvider;

import android.os.Bundle;
import android.widget.TextView;

import com.bangdao.app.jetpacktest.R;

import java.util.Timer;
import java.util.TimerTask;

public class NormalTimerActivity extends AppCompatActivity {

    private TextView tvTime;
    private Timer timer;
    private int currentSecond;

    @Override
    protected void onSaveInstanceState(@NonNull Bundle outState) {
        super.onSaveInstanceState(outState);

//        outState.putInt("currentSecond", currentSecond);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_timer);

//        if(savedInstanceState != null){
//            currentSecond = savedInstanceState.getInt("currentSecond");
//        }else{
            currentSecond = 0;
//        }

        tvTime = findViewById(R.id.tvTime);
        startTiming();
    }

    /**
     * 开始计时
     */
    public void startTiming(){
        if(timer == null){

            timer = new Timer();
            TimerTask timerTask = new TimerTask() {
                @Override
                public void run() {
                    currentSecond++;
                    //更新UI界面
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            tvTime.setText("TIME:" + currentSecond);
                        }
                    });
                }
            };
            timer.schedule(timerTask, 1000, 1000);
        }
    }
}